package com.crm.aspect;

import com.crm.aonnation.Log;
import com.crm.context.BaseContext;
import com.crm.mapper.system.LogMapper;
import com.crm.pojo.OperateLog;
import com.crm.result.ResponseResult;
import com.crm.result.Result;
import com.crm.service.system.LogService;
import com.crm.utils.JwtUtils;
import io.jsonwebtoken.Claims;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.time.LocalDateTime;
import java.util.Arrays;

@Aspect
@Component
public class LogAspect {
    @Autowired
    private LogMapper logMapper;
    @Autowired
    private LogService logService;
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private HttpServletResponse response;
    OperateLog log = new OperateLog();

    @Pointcut("execution(* com.crm.controller..*.*(..))&& @annotation(com.crm.aonnation.Log)")
    public void pointCut() {
    }

    @Around("pointCut()")
    public Object recordLog(ProceedingJoinPoint joinPoint) throws Throwable {

        //操作url
        log.setOperUrl(request.getRequestURI());
        //操作ip
        log.setOperIp("47.108.85.74");
        //操作位置
        log.setOperLocation(InetAddress.getLocalHost().getHostAddress());
        //操作人名称
        log.setOperName(BaseContext.getCurrentUserName());
        //操作时间
        log.setCreateTime(LocalDateTime.now());
        log.setOperTime(LocalDateTime.now());
        log.setCreateBy(String.valueOf(BaseContext.getCurrentId()));
        //类名
        String name = joinPoint.getTarget().getClass().getName();
        log.setOperatorType(name);
        //方法
        log.setMethod(joinPoint.getSignature().getName());//deleteCourse
        String methodName = joinPoint.getSignature().getName();
        //
        if (methodName.contains("add")) {
            log.setBusinessType(1);
            log.setRequestMethod("Post");
        } else if (methodName.contains("delete")) {
            log.setRequestMethod("Delete");
            log.setBusinessType(2);
        } else if (methodName.contains("update")) {
            log.setRequestMethod("Update");
            log.setBusinessType(3);
        } else if (methodName.contains("query")) {
            log.setRequestMethod("Get");
            log.setBusinessType(4);
        }
        log.setRequestMethod(joinPoint.getSignature().getName());
        log.setUpdateBy(BaseContext.getCurrentUserName());
        log.setUpdateTime(LocalDateTime.now());
        //参数
        String pram = Arrays.toString(joinPoint.getArgs());
        log.setOperParam(pram);
        //执行原始方法
        Object proceed = joinPoint.proceed();
        log.setStatus(response.getStatus());
        log.setErrorMsg(ResponseResult.error().toString());
        logService.save(log);
        return proceed;
    }


}
